package ljl.alg.jianzhioffer.round2;

import commons.TreeNode;

import java.util.LinkedList;

public class _27_mirror_of_bi_tree {
    
    /**
    * 写了几亿次了吧也就
    * */
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null;
        TreeNode left = mirrorTree(root.left);
        root.left = mirrorTree(root.right);
        root.right = left;
        return root;
    }
    
    // 笨逼方法也不能忘，层序遍历，然后挨个翻转
    public TreeNode mirrorTree2(TreeNode root) {
        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            if (node.left != null) q.offer(node.left);
            if (node.right != null) q.offer(node.right);
            TreeNode t = node.left;
            node.left = node.right;
            node.right = t;
        }
        return root;
    }
}
